package com.nl.lib_core.tool.extends

import android.graphics.Paint
import android.graphics.Typeface
import android.os.Build
import android.text.Html
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat

/** 作者: wangfei 创建时间: 2021/9/1 包名: com.nl.lib_core.util.extends 类名: TextViewExtends */
/** 设置颜色直接使用colors.xml中定义的颜色即可 */
fun TextView.setColor(resId: Int) {
  this.setTextColor(ResourcesCompat.getColor(resources, resId, null))
}

fun TextView.setDrawableLeft(resId: Int) {
  val drawable = ResourcesCompat.getDrawable(resources, resId, null)
  drawable?.setBounds(0, 0, drawable.minimumWidth - 5, drawable.minimumHeight - 5)
  this.setCompoundDrawables(drawable, null, null, null)
}

fun TextView.setDrawableRight(resId: Int) {
  val drawable = ResourcesCompat.getDrawable(resources, resId, null)
  drawable?.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
  this.setCompoundDrawables(null, null, drawable, null)
}

fun TextView.setDrawableTop(resId: Int) {
  val drawable = ResourcesCompat.getDrawable(resources, resId, null)
  drawable?.setBounds(0, 0, drawable.minimumWidth - 5, drawable.minimumHeight - 5)
  this.setCompoundDrawables(null, drawable, null, null)
}

fun TextView.setDrawableBottom(resId: Int) {
  val drawable = ResourcesCompat.getDrawable(resources, resId, null)
  drawable?.setBounds(0, 0, drawable.minimumWidth - 5, drawable.minimumHeight - 5)
  this.setCompoundDrawables(null, null, null, drawable)
}

fun TextView.setHtmlText(vararg colors: Int) {
  val str = "<font color='${colors[0]}'>只会玩战士</font>回复<font color='${colors[1]}'>冷云他大叔</font>：啊哈哈哈或"
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    this.text = Html.fromHtml(str, Html.FROM_HTML_MODE_LEGACY)
  } else {
    this.text = Html.fromHtml(str)
  }
}

fun TextView.setFontSpannable(color1: Int, color2: Int, colorPosition: Int, content: String) {
  val builder = SpannableStringBuilder(content)
  builder.setSpan(ForegroundColorSpan(color1), 0, colorPosition, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
  builder.setSpan(
      ForegroundColorSpan(color2),
      colorPosition,
      content.length,
      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
  this.text = builder
}

fun TextView.setFontSpannable(
    content: String,
    color1: Int,
    color2: Int,
    color3: Int,
    sub1: Int,
    sub2: Int

) {
  val builder = SpannableStringBuilder(content)
  builder.setSpan(ForegroundColorSpan(color1), 0, sub1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
  builder.setSpan(ForegroundColorSpan(color2), sub1, sub2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
  builder.setSpan(
      ForegroundColorSpan(color3), sub2, content.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
  this.text = builder
}

/** 设置下划线 */
fun TextView.setUnderLine() {
  this.paint.flags = Paint.UNDERLINE_TEXT_FLAG
}

fun TextView.setBold(){
    typeface= Typeface.defaultFromStyle(Typeface.BOLD)
}
